package com.yangguang.huahui.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yangguang.huahui.R;

/**
 * Created by Administrator on 2017/2/15.
 */

public class ProgressDialog extends Dialog {
    private float mDimAmount = 0.5f;
    private LinearLayout mBackgroundLayout;
    private TextView mLabelText;
    private View mView;
    private FrameLayout mCustomViewContainer;
    private String mLabel;
    private int mWidth, mHeight;

    public ProgressDialog(Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.custom_dialog);

        Window window = getWindow();
        window.setBackgroundDrawable(new ColorDrawable(0));
        window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.dimAmount = mDimAmount;
        layoutParams.gravity = Gravity.CENTER;
        window.setAttributes(layoutParams);

        setCanceledOnTouchOutside(false);

        initViews();
    }

    private void initViews() {
        mBackgroundLayout = (LinearLayout) findViewById(R.id.background);
        initBackground(Color.parseColor("#b1000000"), 10);
        mLabelText = (TextView) findViewById(R.id.label);
        if (mLabel != null) {
            mLabelText.setText(mLabel);
            mLabelText.setVisibility(View.VISIBLE);
        } else {
            mLabelText.setVisibility(View.GONE);
        }

        mCustomViewContainer = (FrameLayout) findViewById(R.id.container);
        addViewToFrame(mView);
    }

    public void setLabel(String label) {
        mLabel = label;
        if (mLabelText != null) {
            if (label != null) {
                mLabelText.setText(label);
                mLabelText.setVisibility(View.VISIBLE);
            } else {
                mLabelText.setVisibility(View.GONE);
            }
        }
    }

    public void setView(View view) {
        if (view != null) {
            mView = view;
            if (isShowing()) {
                mCustomViewContainer.removeAllViews();
                addViewToFrame(view);
            }
        }
    }

    public void setSize(Context context, int width, int height) {
        mWidth = width;
        mHeight = height;
        if (mBackgroundLayout != null) {
            updateBackgroundSize(context);
        }
    }

    private void updateBackgroundSize(Context context) {
        ViewGroup.LayoutParams params = mBackgroundLayout.getLayoutParams();
        float scale = context.getResources().getDisplayMetrics().density;
        params.width = (int) (mWidth * scale);
        params.height = (int) (mHeight * scale);
        mBackgroundLayout.setLayoutParams(params);
    }

    private void addViewToFrame(View view) {
        if (view == null) return;
        int wrapParam = ViewGroup.LayoutParams.WRAP_CONTENT;
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(wrapParam, wrapParam);
        mCustomViewContainer.addView(view, params);
    }

    private void initBackground(int color, float cornerRadius) {
        GradientDrawable drawable = new GradientDrawable();
        drawable.setShape(GradientDrawable.RECTANGLE);
        drawable.setColor(color);
        drawable.setCornerRadius(cornerRadius);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            mBackgroundLayout.setBackground(drawable);
        } else {
            //noinspection deprecation
            mBackgroundLayout.setBackgroundDrawable(drawable);
        }
    }
}
